package com.songcy.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.pro.bean.Card_View;
import com.pro.bean.Unit;
import com.pro.bean.Users;
import com.songcy.dao.CardDao;

public class GetCardServlet extends HttpServlet {


	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String name = new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
		String cardname = new String(request.getParameter("cardname").getBytes("ISO-8859-1"),"UTF-8");
		String cmp = new String(request.getParameter("cmp").getBytes("ISO-8859-1"),"UTF-8");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
		String start = request.getParameter("dates");
		String end = request.getParameter("datee");
		String arg = new String(request.getParameter("datearg").getBytes("ISO-8859-1"),"UTF-8");
		CardDao carddao = new CardDao();
		List rs = null;
		if(name.length() != 0)
			rs = carddao.findCards(cmp, cardname, name, arg, start, end);
		else
			rs = carddao.findCards(cmp,cardname,
					((Users)request.getAttribute("Users")).getUsername(),arg,start,end);
		Iterator it = rs.iterator();
		out.println("<!DOCTYPE HTML><head></head><body>");
		String json = "([";
		while(it.hasNext())
		{
			Card_View cardview = (Card_View)it.next();
			json +="{ \"cardid\":\""+cardview.getCardid()+"\",";
			json +="\"name\":\""+cardview.getFirstname()+"\",";
			json +="\"dept\":\""+cardview.getDept()+"\",";
			json +="\"cmp\":\""+cardview.getCmp()+"\",";
			json +="\"username\":\""+cardview.getUsername()+"\",";
			json +="\"unitname\":\""+cardview.getUnitname()+"\"},";
		}
		json += "])";
		out.println(json);
		out.println("</body>");
		request.setAttribute("viewlist", rs);
		//System.out.println(json);
		request.getRequestDispatcher("../scypage/table.jsp").forward(request, response);
//		try {
//			Date start =(Date)sdf.parse(request.getParameter("dates"));
//			Date end =(Date)sdf.parse(request.getParameter("datee"));
//		} catch (ParseException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
//		out.flush();
//		out.close();
	}


}
